<?php

error_reporting(E_ALL);
$system_folder = realpath(dirname(__FILE__)) . '/../';
define('CRON_CI_INDEX', $system_folder . 'index.php'); // Your CodeIgniter main index.php file
define('CRON', TRUE); // Test for this in your controllers if you only want them accessible via cron
ini_set('memory_limit', '128M');

# Parse the command line
$script = array_shift($argv);
$cmdline = implode(' ', $argv);
$usage = "Usage: cron.php --run=/controller/method [--time-limit=N] [--server=http_server_name]\n\n";
$required = array ('--run' => FALSE);
foreach ($argv as $arg) {
   $array = explode('=', $arg);
   if (0 < count($array)) {
      $param = array_shift($array);
   }
   if (0 < count($array)) {
      $value = array_shift($array);
   }
   switch ($param) {
      case '--run':
         // Simulate an HTTP request
         $_SERVER['PATH_INFO'] = $value;
         $_SERVER['ORIG_PATH_INFO'] = $value;
         $_SERVER['REQUEST_URI'] = $value;
         $required['--run'] = TRUE;
      break;
      
      case '--time-limit':
         define('CRON_TIME_LIMIT', $value);
      break;
      
      case '--server':
         $_SERVER['SERVER_NAME'] = $value;
         $_SERVER['HTTP_HOST'] = $value;
      break;
      
      default:
         die($usage);
   }
}

if (!defined('CRON_TIME_LIMIT')) {
   define('CRON_TIME_LIMIT', 0);
}

foreach ($required as $arg => $present) {
   if (!$present) {
      die($usage);
   }
}

# Set run time limit
set_time_limit(CRON_TIME_LIMIT);

chdir(dirname(CRON_CI_INDEX));
require (CRON_CI_INDEX); // Main CI index.php file
